<?php
class appViewAdmin extends coreFrameworkView
{
	function __construct()
	{
		parent::__construct();
	}
	//应用列表（渠道列表）
	function applist()
	{
		$applist = $this->model->getAppList();
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("main_show_block","admin/com_app/app-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加应用
	function addapp(){
		//获取渠道方列表
		$channel_data = $this->model->getChannelListtwo();
		//获取资源方列表
		$resources_data = $this->model->getResourcesList();
		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/app-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//编辑应用
	function editapp(){
		$id = $this->GVar->get['id'];
		//获取资源方列表
		$channel_data = $this->model->getChannelListtwo();
		//获取渠道方列表
		$resources_data = $this->model->getResourcesList();
		//获取应用数据
		$data = $this->model->getChannerData($id);

		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign('data',$data);
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/app-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//生成key
	function createKey(){
		$str = time().rand(10000,99999);
		$key = md5($str);
		echo $key;
	}

	//保存应用
	function saveApp(){
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$post['c_time'] = time();
		$res = $this->model->saveApp($post,$id);
		$url = "index.php?m=admin&o=app&t=applist";
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,null,3);
		
	}
	//查看报表
	function appStatistics(){
		$key = $this->GVar->fget['key'];
		$type = $this->GVar->fget['type'];
		//全国分布
		$datas = $this->model->getChannelLogGroupBy('`key`="'.$key.'"','city');
		$unknow_city_num = 0;
		$all_num = 0;
		foreach ($datas as $k=>$v){
			if (!$v['city']) {
				$unknow_city_num+=$v['num'];
				unset($datas[$k]);
			}
			$all_num+=$v['num'];
		}
		$this->tmp->assign('unknow_city_num',$unknow_city_num);
		$this->tmp->assign('all_num',$all_num);
		$this->tmp->assign('datas',$datas);
		//机型分布
		$mobile_datas = $this->model->getChannelLogGroupBy('`key`="'.$key.'"','mobile_system');
		$this->tmp->assign('mobile_datas',$mobile_datas);
		//时间详细数据
		$time_datas = $this->model->getChannelLog('`key`="'.$key.'"');

		foreach ($time_datas as $v){
			$time_arr[date('Y/m/d',$v['time'])] ++; 
		}
//		foreach ($time_arr as $k=>$v){
//			$time[] = $k;
//			$time_num[] = $v;  
//		}
//
//		$this->tmp->assign('time',$time);
//		$this->tmp->assign('time_num',$time_num);
		$this->tmp->assign('time_arr',$time_arr);
		
		
		$this->tmp->assign("main_show_block","admin/com_app/app-statistics.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//资源管理方列表
	function resources(){
		$resources_data = $this->model->getResourcesList();
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_app/app-resources-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加资源方
	function addresources(){
		$this->tmp->assign("main_show_block","admin/com_app/resources-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//保存资源方信息
	function saveresources(){
		//$get=$this->GVar->fget;
		$post=$this->GVar->fpost;
		if($post['username'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方名称',$url,$re,_lang_save_fail,$url,2);
		}
		if($post['url'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方URL',$url,$re,_lang_save_fail,$url,2);
		}
		$data['resources_name']=$post['username'];
		$data['resources_url']=$post['url'];
		$data['company']=$post['company'];
		$data['contact_name']=$post['contact_name'];
		$data['contact_phone']=$post['contact_phone'];		
		$data['status']=$post['status'];
		if(!$post['id'])
		{
			$data['c_time']=time();
			$this->model->saveResources($data);
		MessageClass::ShowMessage('添加成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}else{
			$this->model->editResources($data,$post['id']);
		MessageClass::ShowMessage('编辑成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}
		
	}
	//编辑资源方信息
	function editresources(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->getResources($get['id']);
		$this->tmp->assign("data",$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/resources-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//删除资源方信息
	function delresources(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->delResources($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
	}
	
	//渠道方列表
	function channelsite()
	{
		$get=$this->GVar->fget;
		$channel_data = $this->model->getChannelList();
		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_channel/channel-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加渠道方
	function addchannel(){
		$this->tmp->assign("main_show_block","admin/com_channel/channel-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//保存渠道方信息
	function savechannel(){
		//$get=$this->GVar->fget;
		$post=$this->GVar->fpost;
		if($post['username'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方名称',$url,$re,_lang_save_fail,$url,2);
		}
		$data['channel_name']=$post['username'];
		$data['company']=$post['company'];
		$data['contact_name']=$post['contact_name'];
		$data['contact_phone']=$post['contact_phone'];		
		$data['status']=$post['status'];
		if(!$post['id'])
		{
			$data['c_time']=time();
			$this->model->saveChannel($data);
		MessageClass::ShowMessage('添加成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}else{
			$this->model->editChannel($data,$post['id']);
		MessageClass::ShowMessage('编辑成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}		
	}
	//编辑渠道方信息
	function editchannel(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->getChannel($get['id']);
		$this->tmp->assign("data",$resources_data);
		$this->tmp->assign("main_show_block","admin/com_channel/channel-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//删除渠道方信息
	function delchannel(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->delChannel($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=channelsite',2);
	}
}

?>